// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import CombatTxtComp from "../Commons/CombatTxtComp";
import { PrefabNames } from "../Enums/GameEnum";
import PoolManager from "./PoolManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TipsTxtManager extends cc.Component {

    private static _single: TipsTxtManager;
    public static get single(): TipsTxtManager {
        return TipsTxtManager._single;
    }

    protected onLoad(): void {
        if (TipsTxtManager._single == null) {
            TipsTxtManager._single = this;
        } else {
            this.node.destroy();
            this.node.removeFromParent();
        }

        cc.game.addPersistRootNode(this.node);
    }

    public showCombatTxt(str: string, pos: cc.Vec2, parentObj: cc.Node, color: cc.Color = cc.Color.WHITE) {
        var txtNode = PoolManager.single.getNode(PrefabNames.CombatTxt);
        if (!txtNode) {
            return;
        }
        var txtComp = txtNode.getComponent(CombatTxtComp);
        txtComp.drawTxt(str, color);
        txtNode.setParent(parentObj);
        txtNode.setPosition(pos);
    }
}
